//app.js
! function() {
  //获取页面配置并进行页面分享配置
  var PageTmp = Page
  Page = function(pageConfig) {
    let view = Page
    //全局开启分享
    pageConfig = Object.assign({
      onShareAppMessage: function() {

      },
      onShareTimeline: function() {

      }
    }, pageConfig);
    //3. 配置页面模板
    PageTmp(pageConfig);
  }
}();
App({
  onLaunch: function (options) {
    console.log("[onLaunch] 本次场景值:", options.scene)
    if(options.scene==1154){
      wx.setStorageSync("ispyq", 1);
    }else{
      wx.setStorageSync("ispyq", 0);
    }

    var self = this;
    //self.overShare()
    wx.checkSession({
      success() {
        wx.getStorage({
          key: 'token',
          success(res){
            self.globalData.token = res.data;
          }
        })
        
      },
      fail() {
        // session_key 已经失效，需要重新执行登录流程
        self.globalData.reLogin(self, self.globalData);
      }
    })
  },
  overShare: function() {
    let self = this
    // 调用隐藏路由回调
    // wx.onAppRoute(function(res) {
    //   //获取加载的页面
    //   let pages = getCurrentPages(),
    //     //获取当前页面的对象
    //     view = pages[pages.length - 1],
    //     data;
    //   if (view) {
    //     data = view.data;
    //     if (true || !data.isOverShare) {
    //       data.isOverShare = true;
    //       view.onShareAppMessage = function() {
    //         //重写分享配置
    //         return {
    //           title: '和善龙江',
    //           imageUrl: 'http://cdn2.kyhdmedia.com/xcx_ljcs/share.jpg',
    //           path:'pages/index/index'
    //         };
    //       },
    //       view.onShareTimeline = function() {
    //         //重写分享配置
    //         return {
    //           title: '和善龙江',
    //           imageUrl: 'http://cdn2.kyhdmedia.com/xcx_ljcs/share.jpg',
    //           path:'pages/index/index'
    //         };
    //       }
    //     }
    //   }
    // })
  },
  token: '',
  globalData: {
    title:'和善龙江',
    userInfo: null,
    token:'',
    openid:'',
    baseUrl: 'https://donate.kyhdmedia.com', //正式
    globalShareInfo: {
      title: '和善龙江',
      path: '/pages/index/index',
      imageUrl: 'https://cdn2.kyhdmedia.com/xcx_ljcs/share.jpg'
    },
    //baseUrl: 'http://donate.test/', //本地测试
    append: {
      login:'api/wx/login',
      list: 'api/default/index',
      detail: 'api/default/detail',
      getAddress: 'api/default/get-address',
      save: 'api/default/save-user-address',
      pay:'api/wx/pay',
      order:'api/default/order-list',
      cancel:'api/wx/cancel',
      info:'api/default/article-info',
      search:'api/default/school-arrs-by-school-name'
    },
    reLogin: function (that,gogal) {
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          console.log(res.code);
          wx.request({
            url: app.globalData.baseUrl + '/api/index/jscode2session',
            data: {
              code: res.code
            },
            header: {
              // "Content-Type": "application/x-www-form-urlencoded" // 默认值
              "Content-Type": "application/json" // 默认值
            },
            method: 'POST',
            success(res) {
              var res = res.data;
              console.log(res);
              if (res.status == 1) {
                wx.setStorage({
                  key: 'token',
                  data: res.data.session_key
                });
                wx.setStorage({
                  key: 'openid',
                  data: res.data.openid
                });
                 gogal.token = res.data.session_key;
                 gogal.openid = res.data.openid;
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      })
    }
  }
})